/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.artezio.buyndrive.validator;

import com.artezio.buyndrive.domain.Turn;
import com.artezio.buyndrive.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

/**
 *
 * @author vgordievich
 */
@Component
public class TurnValidator implements Validator {

    @Autowired
    private UserService userService;

    @Override
    public boolean supports(Class<?> clazz) {
        return Turn.class.isAssignableFrom(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        Turn turn = (Turn) target;

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name",
                "string.notEmpty");
        
        if(turn.getName().length() > 100){
            errors.rejectValue("name", "string.tooLong");
        }

        if(turn.getDescription().length() > 3000){
            errors.rejectValue("description", "string.tooLong");
        }
    }
}